﻿#include "PipelineInfoWid.h"
#include <QMessageBox>
#include "../pipeline/pipeline.h"
#include "componentmanager.h"


PipelineInfoWid::PipelineInfoWid(Pipeline* pipe, QWidget* parent)
	: QWidget(parent), m_pipe(pipe)
{
	ui.setupUi(this);
}

PipelineInfoWid::~PipelineInfoWid()
{}

bool PipelineInfoWid::IsNeedReset()
{
	//if (m_pipe->GetPipelineTaskState() != PipelineTaskState::pipeline_error) {
	//	return false;
	//}
	auto info = m_pipe->GetPipelineTaskInfo();
	if (info.taskId.isEmpty()) {
		return false;
	}
	ui.label_4->setText(info.taskId);
	ui.label_6->setText(QString::number(info.taskNum));
	ui.spinBox->setMaximum(info.taskNum);
	ui.spinBox->setValue(info.finishNum);

	ui.spinBox_2->setValue(info.skuSize.length);
	ui.spinBox_3->setValue(info.skuSize.width);
	ui.spinBox_4->setValue(info.skuSize.height);
	ui.lineEdit->setText(info.skuNo);

	return true;
}

QString PipelineInfoWid::GetName()
{
	ui.label_2->setText(m_pipe->GetPipelineId());
	return m_pipe->GetPipelineId();
}


/*修改任务完成数量*/
void PipelineInfoWid::on_pushButton_clicked()
{
	int num = ui.spinBox->value();
	if (m_pipe->ChangTaskFinishNum(num)) {
		QMessageBox::information(this, "提示", "修改成功");
	} else {
		QMessageBox::warning(this, "提示", "修改成功");
	}
}
/*放弃任务*/
void PipelineInfoWid::on_pushButton_2_clicked()
{
	auto ans = QMessageBox::question(this, "警告",
		"放弃任务后，系统将会删除任务内容。此操作可能会导致机械手与WCS数据不匹配，请确认是否继续？");
	if (ans == QMessageBox::StandardButton::Yes) {
		auto taskP = component->GetComponent(Component::component_taskExcutor);
		QString  id = ui.label_4->text();
		QByteArray output;
		if (!taskP->ExcuteCmd(1, id.toUtf8(), output)) {
			QMessageBox::warning(this, "提示", "从数据库中删除任务失败，请重试");
			return;
		}
		m_pipe->DeleteTaskInfo();
		ui.label_4->setText("");
		this->hide();
	}

}
/*继续执行*/
void PipelineInfoWid::on_pushButton_3_clicked()
{
	m_pipe->TaskContinue();
}
/*任务完成*/
void PipelineInfoWid::on_pushButton_4_clicked()
{
	int num = ui.spinBox->value();
	int task = ui.label_6->text().toInt();
	if (num < task) {
		auto button = QMessageBox::question(this, "提示", 
			"当前的任务完成数量小于任务数量，直接点击任务完成，会给上位机上报当前任务完成失败，请确认是否继续");
		if (button == QMessageBox::No) {
			return;
		}
	}

	if (!m_pipe->ManualTaskFinish()) {
		QMessageBox::warning(this, "提示", "任务完成处理失败");
	} else {
		QMessageBox::warning(this, "提示", "任务已经完成");
		this->hide();
	}
}
